package com.thesis.teacher.controller;

import com.alibaba.fastjson.JSONObject;
import com.thesis.manage.common.constant.YesOrNoEnum;
import com.thesis.manage.common.utils.Result;
import com.thesis.manage.domain.Teacher;
import com.thesis.manage.domain.Thesis;
import com.thesis.manage.service.ThesisService;
import com.thesis.manage.service.model.ExamineEnum;
import com.thesis.manage.service.model.RoleEnum;
import com.thesis.manage.service.model.ThesisTypeEnum;
import com.thesis.manage.service.model.TimeTypeEnum;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author php
 * @date 2018/2/28
 */
@RequestMapping("/thesis")
@RestController
public class ThesisController extends BaseController {
    @Resource
    private ThesisService thesisService;

    /**
     * 查看申请论题状态
     */
    @GetMapping("/application/check")
    public Map<String, Object> checkOpenApplication() {
        return buildMessage(() -> thesisService.checkOpenApplication(TimeTypeEnum.APPLY.getValue()));
    }

    /**
     * 返回论题类型
     */
    @GetMapping("/types")
    public Map<String, Object> getThesisType() {
        return buildMessage(ThesisTypeEnum::getList);
    }

    @PostMapping("/save")
    public Map<String, Object> save(@RequestBody Thesis thesis) {
        return buildMessage(() -> {
            Result result = getUser();
            Teacher teacher = JSONObject.parseObject(result.getData().toString(), Teacher.class);
            thesis.setPersonType(RoleEnum.GUIDETEACHER.getValue());
            thesis.setPerson(teacher.getName());
            thesis.setPersonId(teacher.getId());
            thesis.setCollegeId(teacher.getCollegeId());
            thesis.setPub(YesOrNoEnum.NO.getValue());
            thesis.setStatus(ExamineEnum.LEADE_EXAMINEING.getValue());
            thesisService.save(thesis);
            return null;
        });
    }

    /**
     * 查看自己的选题列表
     */
    @GetMapping("/list")
    public Map<String, Object> list(Thesis thesis, Pageable pageable) {
        return buildMessage(() -> {
            Result result = getUser();
            Teacher teacher = JSONObject.parseObject(result.getData().toString(), Teacher.class);
            thesis.setPersonId(teacher.getId());
            thesis.setPersonType(RoleEnum.GUIDETEACHER.getValue());
            return thesisService.findByTeacher(thesis, pageable);
        });
    }

    @DeleteMapping("/{id}/delete")
    public Map<String, Object> delete(@PathVariable Long id) {
        return buildMessage(() -> {
            thesisService.deleteById(id);
            return null;
        });
    }

    /**
     * 查询待审核的论题
     *
     * @param thesis   论题参数
     * @param pageable 分页参数
     */
    @GetMapping("/review/list")
    public Map<String, Object> reviewList(Thesis thesis, Pageable pageable) {
        return buildMessage(() -> {
            Result result = getUser();
            Teacher teacher = JSONObject.parseObject(result.getData().toString(), Teacher.class);
            thesis.setCollegeId(teacher.getCollegeId());
            thesis.setStatus(ExamineEnum.LEADE_EXAMINEING.getValue());
            return thesisService.reviewList(thesis, pageable);
        });
    }

    /**
     * 通过院系查询论题
     */
    @GetMapping("/college/list")
    public Map<String, Object> findAllByCollege(Thesis thesis, Pageable pageable) {
        return buildMessage(() -> {
            Result result = getUser();
            Teacher teacher = JSONObject.parseObject(result.getData().toString(), Teacher.class);
            thesis.setCollegeId(teacher.getCollegeId());
            //查询通过的
            thesis.setStatus(ExamineEnum.EXAMINE_SUCCESS.getValue());
            return thesisService.findAllByCollege(thesis, pageable);
        });
    }

    /**
     * 论题详情
     *
     * @param id id
     */
    @GetMapping("/{id}")
    public Map<String, Object> detail(@PathVariable Long id) {
        return buildMessage(() -> thesisService.findById(id));
    }

    /**
     * 审核(通过)
     *
     * @param id id
     */
    @PostMapping("/adopt")
    public Map<String, Object> adopt(Long id, @RequestParam(required = false) String message) {
        return buildMessage(() -> {
            thesisService.adopt(id, message);
            return null;
        });
    }

    /**
     * 审核未通过
     *
     * @param id      论题id
     * @param message 评语
     */
    @PostMapping("/unAdopt")
    public Map<String, Object> unAdopt(Long id, String message) {
        return buildMessage(() -> {
            thesisService.unAdopt(id, message);
            return null;
        });
    }

}
